﻿@page "/System/Dictionary"
@attribute [TabItemOption(Text = "数据字典")]
@using System.Diagnostics.CodeAnalysis;
@using BootstrapBlazorApp.Shared.Data.SYS;
@using System.Net.Sockets;
@using WebApiClientCore;
@using WebApiClientCore.Exceptions;
@inject MessageService msg


<Table TItem="DictionaryDto" PageItemsSource="@PageItemsSource" AutoGenerateColumns="true"
       IsPagination="true" IsStriped="true" IsBordered="true" IsMultipleSelect="true"
       ShowToolbar="true" ShowExtendButtons="true" ShowLoading="true" ShowSearch="true" 
       OnAddAsync="@OnAddAsync" OnEditAsync="@OnEditAsync" OnSaveAsync="@OnSaveAsync" OnDeleteAsync="@OnDeleteAsync" @bind-SelectedRows="MySelectedItems"
       OnQueryAsync="@OnQueryAsync" ShowExportButton="true">
    <TableColumns>

    </TableColumns>
</Table>

@code {
    /// <summary>
    /// 当前Token信息
    /// </summary>
    [CascadingParameter]
    private ITokenContext? Context { get; set; }

    private List<DictionaryDto>? SelectedDictionary { get; set; } = new List<DictionaryDto>();

    [NotNull]
    private List<DictionaryDto>? MySelectedItems
    {
        get { return SelectedDictionary; }
        set
        {
            SelectedDictionary = value;
            if(value.Count > 0)
            {
                CurrentDic.DictionaryType = value[0].DictionaryType;
                CurrentDic.DictionaryTypeName = value[0].DictionaryTypeName;
            }

        }
    }

    private static IEnumerable<int> PageItemsSource => new int[] { 4, 10, 20 };

    private DictionaryDto CurrentDic { get; set; } = new DictionaryDto();

    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();

    }


    private async Task<DictionaryDto> OnAddAsync()
    {
        return CurrentDic;
    }

    private async Task<DictionaryDto> OnEditAsync(DictionaryDto item)
    {
        // 模拟延时
        return item;
    }

    private async Task<bool> OnSaveAsync(DictionaryDto item, ItemChangedType changedType)
    {
        if (changedType == ItemChangedType.Add)
        {
            item.Creater = Context.CurrentUser.Account;
            item.CreatedName = Context.CurrentUser.UserName;
            return await Global.ExecuteApiAsync(() => Request.System.AddDictionaryAsync(item));
        }
        else
        {
            item.Modifier = Context.CurrentUser.UserName;
            item.ModifiedName = Context.CurrentUser.UserName;
            return await Global.ExecuteApiAsync(() => Request.System.UpdateDictionaryAsync(item));
        }

    }

    private async Task<bool> OnDeleteAsync(IEnumerable<DictionaryDto> items)
    {
        return await Global.ExecuteApiAsync(() => Request.System.DeleteMutiDictionaryAsync(items.ToList()));
    }

    private async Task<QueryData<DictionaryDto>> OnQueryAsync(QueryPageOptions options)
    {
        try
        {
            //Func委托
            var para = () => Request.System.QueryDictionaryAsync(QueryParameter.GetQueryParameter(options));
            //查询 ExecuteApiAsync负责异常处理
            var model = await Global.ExecuteApiAsync<QueryData<DictionaryDto>>(para);
            return model;
        }
        catch (Exception ex)
        {
            return new QueryData<DictionaryDto>();
        }


    }

   
}
